// pages/tongji/tongji.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 第一行：销售额
    salesData: [
      { title: "今日销售额", amount: "100" },
      { title: "本月销售额", amount: "100" },
      { title: "历史销售额", amount: "100" }
    ],
    // 第二行：设备状态
    statusData: [
      { title: "在线", count: "5" },
      { title: "离线", count: "5" },
      { title: "故障", count: "5" },
      { title: "缺货", count: "5" }
    ],
    // 常用管理
    manageData: [
      { title: "订单管理", icon: "/assets/image/xyy_baoyue.png" },
      { title: "设备管理", icon: "/assets/image/xyy_baoyue.png" },
      { title: "网点管理", icon: "/assets/image/xyy_contact.png" },
      { title: "运维管理", icon: "/assets/image/xyy_baoyue.png" },
      { title: "商品管理", icon: "/assets/image/xyy_yun.png" },
      { title: "营业数据", icon: "/assets/image/xyy_message_center.png" },
      { title: "退款管理", icon: "/assets/image/xyy_dianzi.png" }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getSale()
  },

  getSale(){
    this.requestHttp('/app/client/mine/saleTj', '', 'get').then((data) => {
      console.log(data)
      console.log(data.data)
      this.setData({
        salesData: [
          { title: "今日销售额", amount: data.data.nowDaySale},
          { title: "本月销售额", amount: data.data.nowMonthSale},
          { title: "历史销售额", amount: data.data.allOrderAmount}
        ],
        statusData: [
          { title: "在线", count: data.data.on_line },
          { title: "离线", count: data.data.off_line },
          { title: "故障", count: data.data.breakdown },
          { title: "缺货", count: data.data.stockout }
        ]
      })
    })
  },

  // 点击跳转
  goToPage(e) {
    const index = e.currentTarget.dataset.index;
    console.log(index)
    switch (index) {
      case 0:
        //订单管理
        wx.navigateTo({
          url: '../orderlist/orderlist'
        })
        break;
        case 1:
          //设备管理
          wx.navigateTo({
            url: '../devicemanager/devicemanager'
          })
        break;
        case 2:
          //网点管理
        break;
        case 3:
        //运维管理
        break;
        case 4:
        //商品管理
        wx.navigateTo({
          url: '../shoplist/shoplist'
        })
        break;
        case 5:
        //营业数据
        break;
        case 6:
        //退款管理
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/json'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          const data = res.data;
          if (data.code * 1 === 0) {
            reslove(data)
          } else {
            wx.showToast({
              title: data.msg || '网络异常',
              icon: 'error',
            })
            setTimeout(() => {
              reject(data);
            }, 10)
          }
          // reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})